Standard methods of solution
Objectives : Student should be able to show the understanding about the methods of -
Q1. A programmer writes a program to weigh baskets of fruit in grams, keeping a total of the weight and counting the number of baskets.
a) Explain, including examples of programming statements, how totalling and counting could be used in this program.
The total weight is stored in a variable Total and the number of baskets is stored in a variable BasketCount.
⇒ Initialize the variable "Total" with value 0, to start totalling from it (like, Total ← 0).
⇒ Totalling is done by adding new weight of the basket to the previous old total weight and storing it in the same variable "Total" replacing its previous value during the process of inputing the weight.
⇒ Initialize the variable "BasketCount" with value 0, to start counting from it (like, BasketCount ← 0).
⇒ Counting is done by adding 1 to the previous number of baskets and storing it in same variable "BasketCount"; replacing its previous value during the process of Inputing the weight.
b) Explain with example, how the maximum and minimum weight of the baskets could be found in this program.
The maximum weight is stored in a variable Max and the minimum weight of baskets is stored in a variable Min.
⇒ Initialize the variable "Max" to the lowest possible value (like, Max ← 0).
⇒ Check if the current new weight of the basket is greater than previous maximum value and if so then store the new weight in the same variable "Max"; replacing its previous value during the process of inputing the weight.
⇒ Initialize the variable "Min" to the highest possible value (like, Min ← 1000).
⇒ Check if the current new weight of the basket is less than previous minimum value and if so then store the new weight in the same variable "Min"; replacing its previous value during the process of inputing the weight.
c) Explain with example, how the average weight of the baskets could be calculated in this program.
The average weight is stored in a variable Avg.
⇒ Calculate the running total of the weight and count the number of weight of the baskets during the process of inputing the weight.
⇒ To calculate average, divide the total weight by number of weight of the baskets (outside the loop).
Q2. An algorithm has been written in pseudocode to :
01A ‹— 0
02B ‹— 0
03C ‹— 0
04REPEAT
05REPEAT
06INPUT D
07UNTIL D > 0 AND D < 100 AND D = INT(D)
08IF D > B
09THEN
10B ‹— D
11ENDIF
12C ‹— C + D
13A ‹— A + 1
14UNTIL A >= 25
15B ‹— C / A
16OUTPUT "Largest number is ", B
17OUTPUT "Average is ", E
Give the line number for the statements showing :
Totalling : Line 12 : C ‹— C + D
Counting : Line 13 : A ‹— A + 1
Range check : Line 07 : UNTIL D > 0 AND D < 100
Calculating the average : Line 15 : B ‹— C / A
Q3. a) Write a pseudocode algorithm to input the mark scored by 15 students of a class. Find and output the highest and lowest mark scored by students.
Highest ← 0
Lowest ← 100
FOR Count = 1 TO 15
INPUT "Enter the mark : ", Mark
IF Mark > Highest THEN Highest ← Mark
IF Mark < Lowest THEN Lowest ← Mark
NEXT Count
OUTPUT "The highest mark scored by students is ", Highest
OUTPUT "The lowest mark scored by students is ", Lowest
b) Re-write the above program of part(a) to count and output the number of students with same highest and lowest marks.
Highest ← 0
Lowest ← 100
HighCount ← 0
LowCount ← 0
FOR Count = 1 TO 15
INPUT "Enter the mark : ", Mark
IF Mark = Highest THEN HighCount ← HighCount + 1
IF Mark > Highest THEN
Highest ← Mark
HighCount ← 1
ENDIF
IF Mark = Lowest THEN LowCount ← LowCount + 1
IF Mark < Lowest THEN
Lowest ← Mark
LowCount ← 1
ENDIF
NEXT Count
OUTPUT "There are ", HighCount, " students with the highest mark of ", Highest
OUTPUT "There are ", LowCount, " students with the lowest mark of ", Lowest
Q4. Write a program to input the temperature of a day measured at an interval of 3 hour. Assume that the temperature could be between -500C to +500C.
Find and output the maximum, minimum and average temperature of the day.
Count and output the number of times the temperature were less than or equal to 0.
MaxTemp ← - 50
MinTemp ← 50
CountTemp ← 0
Sum ← 0
'// Repeat 8-times to input temp at an interval of 3 hour for the day of 24 hours (i.e. 24/3)
FOR X = 1 TO 8
INPUT "Enter the temperature : ", Temp
IF Temp > MaxTemp THEN MaxTemp ← Temp
IF Temp < MinTemp THEN MinTemp ← Temp
IF Temp <= 0 THEN CountTemp ← CountTemp + 1
Sum ← Sum + Temp
NEXT X
Avg ← Sum / 8
OUTPUT "The maximum temperature of the day is ", MaxTemp
OUTPUT "The minimum temperature of the day is ", MinTemp
OUTPUT "The average temperature of the day is ", Avg
OUTPUT CountTemp, "-times the temperature was less than or equal to 0."
► Linear Search : A the method of checking each item of the list in turn to see if the item matches the value searched for.
Q5. Write a pseudocode algorithm to input the name of the student to search in the list of names stored the array StdMark[ ], if found output his location in array and if not output an error message. The number of students the class is stored in the variable ClassSize.
'// Setting a variable as flag, to indicate if the name has been found TRUE or FALSE.
Found ← "FALSE"
Counter ← 1
INPUT "Please enter the name to find - ", Name
REPEAT
IF StdName[Counter] = Name THEN
Found ← "TRUE"
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found = "TRUE" OR Counter > ClassSize
'// Stop the search when a match is found or the whole list has been searched.
IF Found = "TRUE" THEN
OUTPUT Name, " found at position ", Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF
Q6. Write a pseudocode algorithm to search and check how many students has passed in a list of marks, store in an array StdMark[ ]. The number of students is stored in the variable ClassSize. The pass mark is above or equal to 60.
NumOfPass ← 0
FOR Counter = 1 TO ClassSize
IF StdMark[Counter] >= 60 THEN
NumOfPass ← NumOfPass + 1
ENDIF
NEXT Counter
OUTPUT NumOfPass, " students have passed in their exam."
Q7. The name and mark of 25 students are stored in two different array StdName[ ] and StdMark[ ]. The position of each student's data in the two arrays is the same, for example, the student in position 10 in StdName[ ] and StdMark[ ] is the same.
Write a pseudocode algorithm to find the student who scored the highest and the lowest mark, and output both their name and mark scored.
Highest ← 0
Lowest ← 100
FOR Counter = 1 TO 25
'// Find and store the highest mark and it's position in the array (i.e. index value of array)
IF StdMark[Counter] > Highest THEN
Highest ← StdMark[Counter]
HighIndex ← Counter
ENDIF
'// Find and store the lowest mark and it's position in the array (i.e. index value of array)
IF StdMark[Counter] < Lowest THEN
Lowest ← StdMark[Counter]
LowIndex ← Counter
ENDIF
NEXT Counter
OUTPUT StdName[HighIndex], " scored the highest mark of ", Highest
OUTPUT StdName[LowIndex], " scored the lowest mark of ", Lowest
► Bubble Sort :
Q8. Describe Bubble sort algorithm.
⇒ Bubble Sort is an algorithm for arranging a series of numbers or other elements in the correct order.
⇒ The method works by comparing each set of adjacent elements of the entire list, from left to right, swapping their positions if they are out of order.
⇒ The algorithm then repeats this process until it can run through the entire list without swapping any elements.
Q9. a) Write a procedure using pseudocode to input and store 25 integers.
Sort and output the numbers in ascending order.
DECLARE Num : ARRAY[1:25] OF Integer
'// Input and store 25 integers in an array.
FOR X ← 1 to 25
INPUT Num[X]
NEXT X'// Sort the numbers by swapping the integers between the elements of the array,
'// if the value of next element is less than the current element.
REPEAT'// Setting a variable "Swap" as flag, to indicate that swapping of element is made or not.
'// swap = 0 means, numbers not swapped, and swap = 1 means swapped.
Swap ← 0
'// Repeat One-time less the the size of the array (i.e. 25 - 1 = 24),
'// as we compare the current value with the next value of array element.
FOR Y ← 1 to 24IF Num[Y+1] < Num[Y] THEN
Temp ← Num[Y]
Num[Y] ← Num[Y+1]
Num[Y+1] ← Temp
Swap ← 1
ENDIFNEXT Y
UNTIL Swap = 0
'// Output the numbers in Ascending order from sorted list stored in array.
FOR Z ← 1 to 25
OUTPUT Num[X]
NEXT Z
b) Explain what changes has to be made in your program in part(a) to sort and output the numbers in descending order .
⇒ Change the conditional statement - "IF Num[Y+1] < Num[Y]" to "IF Num[Y+1] > Num[Y]" which will swap the numbers only if the next number is greater than the present number.
This swapping of numbers has to be done repeatedly until no further swapping is needed, to sort it in descending order.
Q10. A list of name of 5 persons is stored in an array PeopleName[ ].
a) Write a program to sort and output the names in ascending order.
REPEAT
Swap ← 0
FOR X ← 1 TO 4
IF PeopleName[X+1] < PeopleName[X] THEN
Temp = PeopleName[X]
PeopleName[X] = PeopleName[X+1]
PeopleName[X+1] = Temp
Swap ← 1
ENDIF
NEXT X
UNTIL Swap ← 0
FOR Y ← 1 TO 5
OUTPUT PeopleName[Y]
NEXT Y
b) The table below shows the contents of the array PeopleName[ ], e.g. PeopleName[3] stores the name Jose.
Array | PeopleName | ||||
Index | [1] | [2] | [3] | [4] | [5] |
Value | Daniel | Alex | Jose | Bob | Monty |
Complete the trace table for your program of part (a) to sort the names in ascending order.
Loop Counter | PeopleName | Temp | Swap | ||||
[1] | [2] | [3] | [4] | [5] | |||
Daniel | Alex | Jose | Bob | Monty | 0 | ||
1 | Alex | Daniel | " | " | " | Daniel | 1 |
2 | " | " | " | " | " | " | " |
3 | " | " | Bob | Jose | " | Jose | 1 |
4 | " | " | " | " | " | " | " |
Alex | Daniel | Bob | Jose | Monty | " | 0 | |
1 | " | " | " | " | " | " | " |
2 | " | Bob | Daniel | " | " | Daniel | 1 |
3 | " | " | " | " | " | " | " |
4 | " | " | " | " | " | " | " |
Alex | Bob | Daniel | Jose | Monty | " | 0 | |
1 | " | " | " | " | " | " | " |
2 | " | " | " | " | " | " | " |
3 | " | " | " | " | " | " | " |
4 | " | " | " | " | " | " | " |
Show the content of your array Mark after sorting process.
Array | PeopleName | ||||
Index | [1] | [2] | [3] | [4] | [5] |
Value | Alex | Bob | Daniel | Jose | Monty |
c) Explain what changes has to be made in your program in part(a) to sort and output the numbers in descending order .
⇒ Change the conditional statement - "IF Num[X+1] < Num[X]" to "IF Num[X+1] > Num[X]" which will swap the names only if the next name is greater than the present name.
This swapping of names has to be done repeatedly until no further swapping is needed, to sort it in descending order.
* * * * * * * * *
* * * * * *
* * *
*